package net.sahv.bdyz.controller.admin;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import net.sahv.bdyz.model.HelpCenter;
import net.sahv.bdyz.service.HelpCenterService;

@Controller
@RequestMapping("admin/helpCenter")
public class HelpCenterController {

	@Autowired
	private HelpCenterService helpCenterService;
	
	/**
	 * 跳转到帮助中心页面
	 * @author 张天敏
	 * @date 日期：2016年8月17日 时间：下午2:01:11
	 * @param model
	 * @param state   
	 * @return
	 */
	@RequestMapping(value="/helpCenterDetails",method=RequestMethod.GET)
	public String helpCenterDetails(Model model,Integer state){
		HelpCenter helpCenter=helpCenterService.selectDetails(state);
		if(helpCenter == null){
			model.addAttribute("state", state);
			//centerState默认有两种状态：0或1。0、表示新增，1、表示修改。
			model.addAttribute("centerState",0);
			model.addAttribute("helpCenter", helpCenter);
			return "admin/helpCenter/helpCenterDetails";			
		}else{
			//centerState默认有两种状态：0或1。0、表示新增，1、表示修改。
			model.addAttribute("centerState",1);
			model.addAttribute("state", state);
			model.addAttribute("helpCenter", helpCenter);
			return "admin/helpCenter/helpCenterDetails";	
		}
	}
	
	/**
	 * 帮助中心
	 * @author 张天敏
	 * @date 日期：2016年8月17日 时间：下午1:58:09
	 * @param editorValue
	 * @param state  
	 * @param centerState   centerState默认有两种状态：0或1。0、表示新增，1、表示修改。
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/helpCenterDetails",method=RequestMethod.POST)
	public ApiResult helpCenterDetails(HttpServletRequest request,Integer state,Integer centerState){
		HelpCenter helpCenter=helpCenterService.selectDetails(state);
		String [] editorValues=request.getParameterValues("editorValue");
		ApiResult result=new ApiResult();
		if(centerState==0){
			HelpCenter help=new HelpCenter();
			help.sethContent(editorValues[0]);
			help.sethContentEn(editorValues[1]);
			help.sethType(state);
			int i=helpCenterService.insert(help);
			if(i ==1){
				 result.msg.status=0;
				 result.msg.desc="添加成功！";
			}else{
				result.msg.status=1;
				result.msg.desc="添加失败！";
			}
		}
		if(centerState==1){
			HelpCenter help=new HelpCenter();
			help.sethContent(editorValues[0]);
			help.sethContentEn(editorValues[1]);
			help.sethType(state);
			help.sethId(helpCenter.gethId());
			int i=helpCenterService.updateByPrimaryKey(help);
			if(i ==1){
				 result.msg.status=0;
				 result.msg.desc="修改成功！";
			}else{
				result.msg.status=1;
				result.msg.desc="修改失败！";
			}
		}
		return result;
	}
}
